home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14263 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  86 lines

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP: Code in Header Files
  5. Date: Fri, 29 Mar 1996 13:02:00 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <315C2598.4B2A@datalytics.com>
  8. References: <4jakhm$g3d@news.asu.edu>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. ryan1m@imap2.asu.edu wrote:
  16. > I have a problem understanding how header files work.  If it matters I am
  17. > using Borland C++ 4.5...
  18. > The problem is I defined a header file   string.h  in this I declared a
  19. > string class.  If I put the actual code to the functions in this class
  20. > into the header file, the compiler does not like it.  I looked at other
  21. > .h files and saw they did not do it this way.
  22. > So then I tried writing the actual code into string.cpp and compiling
  23. > that with and #include "string.h".
  24. > It worked but my code for my program does not recognize the functions.
  25. > It says I did not code them yet.
  26. > It finds the headers just fine but not the actual code.
  27. > ie:
  28. > #include "string.h"
  29. > void main(void)
  30. > {
  31. >     String blah("hello world");
  32. > }
  33. > the error will say something like
  34. > Undefined Symbol String::~String() in module STEST.CPP
  35. > and it goes on to list the rest of the function not "defined".
  36. > Please help...
  37.  
  38. You have to link with the object code generated for your string 
  39. class.  There are several approaches to doing that.  The 
  40. simplest is to add string.cpp to your project.  The compiler 
  41. will compile that code too, and the linker will then add that 
  42. object code to your application.
  43.  
  44. While that was the simplest approach, it doesn't allow you to 
  45. reuse string.cpp reliably.  You could add string.cpp to each of 
  46. your applications, but you would probably do so by making a 
  47. copy.  With this approach, you'd end up with many copies of 
  48. string.cpp (and probably string.h too), so any changes you make 
  49. must be done to every copy.
  50.  
  51. The first approach to reusing the code is to put it into a 
  52. static library, a .LIB file.  You create a new project in BC to 
  53. generate a static library, make string.cpp the one file in the 
  54. project, then build your library.  (You would probably name the 
  55. output string.lib.)  Now, if you return to your application's 
  56. project, tell the linker to link with string.lib (be sure 
  57. string.cpp is not part of the project).  The linker will now 
  58. locate string.lib, scan it for object files with the necessary 
  59. code, and link that code into your application.
  60.  
  61. You can get even fancier by building a DLL.  A dynamic link 
  62. library is essentially the same as a static library, except the 
  63. "linking" stuff doesn't occur until load time.  When the OS 
  64. loads your application, it will find references to the DLL and 
  65. will patch stub calls, to the functions in the DLL, with the 
  66. correct address within the DLL.  Once the loader is finished, 
  67. you'll have a runtime configuration essentially the same as you 
  68. got at link time with a static library.  The DLL code 
  69. essentially becomes part of your application while it runs.
  70.  
  71. -- 
  72. Robert Stewart        | My opinions are usually my own.
  73. Datalytics, Inc.    | stew@datalytics.com
  74.